﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class PublicGroupDataFolderProcessBll
    {
        public static PublicGroupDataFolderCls CreatePublicGroupDataFolderModel(string SiteId, string PublicGroupDataFolderId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePublicGroupDataFolder " +
                                               "     where PublicGroupDataFolderId=@PublicGroupDataFolderId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("PublicGroupDataFolderId", PublicGroupDataFolderId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            PublicGroupDataFolderCls OPublicGroupDataFolder = PublicGroupDataFolderParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OPublicGroupDataFolder;
        }



        public static PublicGroupDataFolderCls[] ReadingPublicGroupDataFolder(string SiteId, string GroupDataId, string ParentPublicGroupDataFolderId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query="select * from TablePublicGroupDataFolder where frkGroupDataId='" + GroupDataId + "' ";
            if (!string.IsNullOrEmpty(ParentPublicGroupDataFolderId))
            {
                Query += " and frkParentPublicGroupDataFolderId='" + ParentPublicGroupDataFolderId + "'";
            }
            else 
            {
                Query += " and frkParentPublicGroupDataFolderId is null";
            }
            Query += " order by SortIndex";
            DataSet dsResult = DBService.GetDataSet(null, Query, null);
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new PublicGroupDataFolderCls[0];
            }
            PublicGroupDataFolderCls[] PublicGroupDataFolder = PublicGroupDataFolderParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return PublicGroupDataFolder;
        }


        public static PublicGroupDataFolderCls[] ReadingPublicGroupDataFolderForUser(string SiteId, string GroupDataId, string UserId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query = " select * from TablePublicGroupDataFolder where frkGroupDataId='" + GroupDataId + "' and frkParentPublicGroupDataFolderId is null "+
                           "    and "+
                           "    ("+
                           "        PublicGroupDataFolderId in "+
                           "        ("+
                           "            select ObjectId from TablePermissionRole"+
                           "                where frkPermissionTypeId='DocumentFolder' and type='user' and frkUserId='"+UserId+"' "+
                           "        )"+
                           "        or"+
                           "        PublicGroupDataFolderId in"+
                           "        ("+
                           "            select ObjectId from TablePermissionRole" +
                           "                where frkPermissionTypeId='DocumentFolder' and type='role'" +
                           "                and frkRoleId in "+
                           "                ("+
                           "                    select frkRoleId from TableUserRole"+
                           "                        where TableUserRole.frkUserId='"+UserId+"'"+
                           "                )"+
                           "        )"+
                           "    )"+
                           "  order by SortIndex";
            DataSet dsResult = DBService.GetDataSet(null, Query, null);
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new PublicGroupDataFolderCls[0];
            }
            PublicGroupDataFolderCls[] PublicGroupDataFolder = PublicGroupDataFolderParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return PublicGroupDataFolder;
        }

        public static void AddPublicGroupDataFolder(
            string SiteId,
            string GroupDataId,
            string FolderName,
            string Description,
            string ParentPublicGroupDataFolderId,
            string SortIndex)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string PublicGroupDataFolderId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            if (string.IsNullOrEmpty(ParentPublicGroupDataFolderId))
            {
                ParentPublicGroupDataFolderId = null;
            }
            DBService.Insert(null, "TablePublicGroupDataFolder",
                    new DbParam[] 
                    { 
                        new DbParam("PublicGroupDataFolderId", PublicGroupDataFolderId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("FolderName", FolderName),
                        new DbParam("Description", Description),
                        new DbParam("frkParentPublicGroupDataFolderId", ParentPublicGroupDataFolderId),
                        new DbParam("SortIndex", SortIndex),
                    });
        }


        public static void SavePublicGroupDataFolder(string SiteId, string PublicGroupDataFolderId, string FolderName, string Description, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TablePublicGroupDataFolder", "PublicGroupDataFolderId", PublicGroupDataFolderId,
                    new DbParam[] 
                    { 
                        new DbParam("FolderName", FolderName),
                        new DbParam("Description", Description),
                    });
        }


        public static void DeletePublicGroupDataFolder(string SiteId, string PublicGroupDataFolderId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDataBelongPublicGroupDataFolder where frkPublicGroupDataFolderId=@PublicGroupDataFolderId; "+
                " Delete from TablePublicGroupDataFolder where PublicGroupDataFolderId=@PublicGroupDataFolderId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("PublicGroupDataFolderId", PublicGroupDataFolderId),
                    });
        }



    }
}
